/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa036is.validators;

import cz.muni.fi.pa036is.dto.PersonFormTO;
import cz.muni.fi.pa036is.entity.Person;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

/**
 *
 * @author Kuba
 */
public class PersonValidator implements Validator{

    @Override
    public boolean supports(Class<?> type) {
        return Person.class.equals(type);
    }

    @Override
    public void validate(Object o, Errors errors) {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "firstName", "firstName.empty");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "lastName", "lastName.empty");
        ValidationUtils.rejectIfEmpty(errors, "dateOfBirth", "dateOfBirth.empty");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "phoneNumber", "phoneNumber.empty");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "email", "email.empty");
        
        PersonFormTO person = (PersonFormTO) o;

        if (person.getFirstName().length() > 100) {
            errors.rejectValue("firstName", "Attribute First Name is too long.");
        }

        if (person.getLastName().length() > 100) {
            errors.rejectValue("lastName", "Attribute Last Name is too long.");
        }
        
        if (person.getPhoneNumber().length() > 20) {
            errors.rejectValue("phoneNumber", "Attribute Phone Number is too long.");
        }
        
        if (person.getEmail().length() > 100) {
            errors.rejectValue("email", "Attribute Email is too long.");
        }
        
       
    }
    
}
